package deleteface.bank.jqsoft.cn.deleteface.setip;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.roundview.RoundTextView;


import org.litepal.crud.DataSupport;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jqsoft.bank.deleteface.R;


//更改添加服务器
public class SettingServerChangeActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.treatment_title)
    TextView treatment_title;
    @BindView(R.id.ip)
    EditText ed_ip;
    @BindView(R.id.d_ip)
    EditText ed_dip;

    @BindView(R.id.termip)
    EditText ed_termip;
    @BindView(R.id.d_termip)
    EditText ed_termdip;

    @BindView(R.id.address)
    EditText ed_address;
    @BindView(R.id.btn_save)
    RoundTextView btn_save;
    private SettingServerAdapter mAdapter;
    SettingServerBean   settingServerBean;
    String SettingStyle="0";
    private static String CHANGESERVER="1";
    private static String ADDSERVER="2";
    int Checkposition;
    List<SettingServerBean> Serverlist = DataSupport.findAll(SettingServerBean.class);
    private   int repeatipflag=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_serverchange);
        ButterKnife.bind(SettingServerChangeActivity.this);
        initData();
        initView();


    }

    public Serializable getDeliveredSerializableByKey(String key) {
        if (TextUtils.isEmpty(key)) {
            return null;
        } else {
            key = Util.trimString(key);
            Intent intent = getIntent();
            if (intent == null) {
                return null;
            } else {
                Serializable result = intent.getSerializableExtra(key);
                return result;
            }
        }
    }
    protected void initData() {
        SettingStyle=(String)getDeliveredSerializableByKey(Constants.SETTING_SERVER_ACTIVITY_STYE_KEY);
    }

    @SuppressLint("ResourceAsColor")
    protected void initView() {
        setToolBar(toolbar, "");
        treatment_title.setText("设置服务器");
        if (SettingStyle.equals(ADDSERVER)){

        }else if (SettingStyle.equals(CHANGESERVER)){
            settingServerBean=(SettingServerBean)getDeliveredSerializableByKey(Constants.SETTING_SERVER_ACTIVITY_KEY);

            Checkposition=(int)getDeliveredSerializableByKey(Constants.SETTING_SERVER_POSTION_ACTIVITY_KEY);
            ed_address.setTextColor(R.color.black_new);
            ed_address.setText(settingServerBean.getAddress());
            String ipstr= settingServerBean.getIp();

           if (isIp(ipstr)){
        try {
            String ip=ipstr.substring(0,ipstr.indexOf(":"));
            ed_ip.setTextColor(R.color.black_new);
            ed_dip.setTextColor(R.color.black_new);

            ed_ip.setText(ip);

            String dip=ipstr.substring(ipstr.indexOf(":")+1 ,ipstr.length());

            ed_dip.setText(dip);

        }catch (Exception e){
            Toast.makeText(this,"ip地址格式不正确", Toast.LENGTH_SHORT).show();

        }
           }else {
               Toast.makeText(this,"ip地址格式不正确", Toast.LENGTH_SHORT).show();
           }


        }
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String et_ip=ed_ip.getText().toString().trim();
                String ed_dim_ip=ed_termip.getText().toString().trim();
                String ed_port=ed_dip.getText().toString().trim();
                String ed_trim_port=ed_termdip.getText().toString().trim();
                String saddress=ed_address.getText().toString().trim();
                if(TextUtils.isEmpty(et_ip)){
                    Toast.makeText(SettingServerChangeActivity.this,"人脸服务器地址不能为空",Toast.LENGTH_LONG).show();
                }else if(TextUtils.isEmpty(ed_port)){
                    Toast.makeText(SettingServerChangeActivity.this,"人脸服务器端口不能为空",Toast.LENGTH_LONG).show();
                }else if(TextUtils.isEmpty(ed_dim_ip)){
                    Toast.makeText(SettingServerChangeActivity.this,"有效期地址不能为空",Toast.LENGTH_LONG).show();
                }else if(TextUtils.isEmpty(ed_trim_port)){
                    Toast.makeText(SettingServerChangeActivity.this,"有效期端口不能为空",Toast.LENGTH_LONG).show();
                }else if(TextUtils.isEmpty(saddress)){
                    Toast.makeText(SettingServerChangeActivity.this,"备注不能为空",Toast.LENGTH_LONG).show();
                }else {
                    String ip= ed_ip.getText().toString();
                    String dip= ed_dip.getText().toString();
                    String address=ed_address.getText().toString();
                    String name="";
                    String ipstr=ip+":"+dip;

                    String termip= ed_termip.getText().toString();
                    String termdip= ed_termdip.getText().toString();
                    String termipstr=termip+":"+termdip;
                    //判断ip重复

                    if (isIp(ipstr) || isIp(termipstr)) {


                        if (SettingStyle.equals(ADDSERVER)) {
                            addrepeatip(ipstr);

                                SettingServerBean bean = new SettingServerBean(ipstr, termipstr,address, name, "0");
                                bean.setAddress(address);
                                bean.setIp(ipstr);
                                bean.setUsername(name);
                                if (bean.save()) {

                                    Toast.makeText(SettingServerChangeActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {
                                    Toast.makeText(SettingServerChangeActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                                }


                        } else if (SettingStyle.equals(CHANGESERVER)) {
                            changerepeatip(ipstr);

                                ContentValues values = new ContentValues();
                                values.put("address", address);
                                values.put("ip", ipstr);
                                values.put("termip", termipstr);
                                values.put("username", name);
                                DataSupport.update(SettingServerBean.class, values, Checkposition + 1);
                                Serverlist = DataSupport.findAll(SettingServerBean.class);
                                Toast.makeText(SettingServerChangeActivity.this, "更改成功", Toast.LENGTH_SHORT).show();
                                finish();


                        }

                    }else {
                        Toast.makeText(SettingServerChangeActivity.this,"地址格式不正确,请检测人脸或者有效期格式", Toast.LENGTH_SHORT).show();
                    }


                }




            }
        });





    }


    public void setToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);//显示toolbar的返回按钮
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        View backView = toolbar.findViewById(R.id.ll_back);
        if (backView != null) {
            backView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
////        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View view) {
////                onBackPressed();
////            }
////        });
    }

    protected void loadData() {




    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public static boolean isIp(String text) {
        if (text != null && !text.isEmpty()) {
            // 定义正则表达式
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."+
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"+
                    ":([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])$";
            // 判断ip地址是否与正则表达式匹配
            if (text.matches(regex)) {
                // 返回判断信息
                return true;
            } else {
                // 返回判断信息
                return false;
            }
        }
        return false;
    }
private  void addrepeatip(String ip){
    for (int i =0;i<Serverlist.size();i++){

        if (Serverlist.get(i).getIp().equals(ip)){

            repeatipflag=1;

        }

    }

}
private void   changerepeatip(String ip){
    for (int i =0;i<Serverlist.size();i++){

        if (Serverlist.get(i).getIp().equals(settingServerBean.getIp())){

        }else   if (Serverlist.get(i).getIp().equals(ip)){


            repeatipflag=1;

        }



    }


}

}